package com.gexiaobao.pigeon.ui.home.activity

import android.os.Bundle
import com.gexiaobao.pigeon.app.base.BaseActivity
import com.gexiaobao.pigeon.app.model.bean.GPSRingInfoResponse
import com.gexiaobao.pigeon.app.util.LayerTileSources
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.ActivityGpsMapTrackBinding
import com.gexiaobao.pigeon.permissions.PermissionInterceptor
import com.gexiaobao.pigeon.viewmodel.MyTrainingViewModel
import com.google.gson.Gson
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import org.osmdroid.util.GeoPoint
import org.osmdroid.views.CustomZoomButtonsController

/**
 * @Author      : hxw
 * @Date        : 2024/12/30 9:26
 * @Describe    : gps轨迹
 */
class ActivityGPSMapTrack : BaseActivity<MyTrainingViewModel, ActivityGpsMapTrackBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        getPermission()// 获取权限

        onRefresh()
    }

    private fun onRefresh() {
        val mTrainingId = intent.getIntExtra("trainingId", 0)
        val gpsSns = intent.getStringArrayListExtra("gpsSns")
        val map = mapOf("trainingId" to mTrainingId, "gpsSns" to gpsSns)
        val body: RequestBody = Gson().toJson(map).toRequestBody("application/json".toMediaTypeOrNull())
        mViewModel.getGpsRingInfosForTraining(body)
    }

    override fun initView(savedInstanceState: Bundle?) {
        StatusBarUtil.setTranslucentForImageView(this, 0, null)
        mDatabind.includeBar.tvTitle.text = "轨迹"

    }

    override fun createObserver() {
        super.createObserver()
        /** 获取训放gps轨迹 */
        mViewModel.ringInfosResult.observe(this) {
            initMap(it)
        }
    }

    private fun getPermission() {
        XXPermissions.with(this)
            .permission(Permission.ACCESS_FINE_LOCATION)
            .permission(Permission.ACCESS_COARSE_LOCATION)
            .interceptor(PermissionInterceptor())
            .request { _, allGranted ->
                if (allGranted) {
//                    initMap()
                } else {
                    RxToast.error("权限不足，地图加载失败！")
                }
            }
    }

    private fun initMap(data: GPSRingInfoResponse) {
        val startLon = data.flyLon
        val startLat = data.flyLat
        mDatabind.mapView.setUseDataConnection(true)
        // 加载在线地图-高德地图
        mDatabind.mapView.setTileSource(LayerTileSources.AutoNaviVector)
        // 加载天地图影像图
//        mDatabind.mapView.setTileSource(LayerTileSources.TDTIMG_W)
        // 加载天地图标注图
//        val tilesOverlay = TilesOverlay(MapTileProviderBasic(this, LayerTileSources.TDTCIA_W), this)
//        mDatabind.mapView.overlayManager.add(tilesOverlay)

        // 设置最小缩放比例
        mDatabind.mapView.minZoomLevel = 3.0
        // 设置最大缩放比例
        mDatabind.mapView.maxZoomLevel = 18.0
        // 让瓦片适应不同像素密度:默认地图显示的字体小，图片像素高，可设置以下代码，使地图适应不同像素密度，更美观
//        mDatabind.mapView.setTilesScaledToDpi(true)

        val mapController = mDatabind.mapView.controller
        // 设置地图初始级别
        mapController.setZoom(9.0)

        // 设置初始中心点
        val centerPoint = GeoPoint(startLat, startLon)
        mapController.setCenter(centerPoint)

        //启用缩放及滑动手势
        mDatabind.mapView.zoomController.setVisibility(CustomZoomButtonsController.Visibility.NEVER)
        mDatabind.mapView.setMultiTouchControls(true)
        mDatabind.mapView.overlayManager.tilesOverlay.isEnabled = true
        mDatabind.mapView.isSelected = true
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> finish()
            }
        }
    }
}